# Definition for singly-linked list.
class ListNode:
    def __init__(self, x):
        self.val = x
        self.next = None

class Solution:
    def hasCycle(self, head: ListNode) -> bool:
        slow = head
        if slow is None or slow.next is None:
            return False
        fast = slow.next
        while fast:
            if fast == slow or fast.next == slow:
                return True
            if fast.next is None:
                return False
            fast = fast.next.next
            slow = slow.next
        return False

if __name__ == '__main__':
    so = Solution()